package com.core.shared;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.core.model.PaginationInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: xf
 * Date: 2017-12-06
 * Time: 21:45
 * To change this template use Editor | File and Code Templates.
 */
public abstract class WebUtil {

    /**
     * 获取当前线程对应的http请求对象
     *
     * @return
     */
    public static HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
    }

    /**
     * 获取session
     *
     * @return
     */
    public static HttpSession getSession() {
        return getRequest().getSession();
    }

    /**
     * 获取当前线程的request请求参数
     *
     * @return
     */
    public static Map<String, String> getCurrentParameterMap() {
        return getParameterMap(getRequest());
    }

    /**
     * 获取当前线程对应request的Locale
     *
     * @return
     */
    public static Locale getCurrentLocale() {
        Locale locale = getRequest().getLocale();
        if (locale.equals(Locale.SIMPLIFIED_CHINESE)) {
            locale = Locale.CHINESE;
        }
        return locale;
    }

    /**
     * 获取参数的map
     *
     * @param request the request
     * @return the parameter map
     */
    @SuppressWarnings("unchecked")
    public static Map<String, String> getParameterMap(HttpServletRequest request) {
        Map<String, String> parameterMap = new HashMap<String, String>();
        Set<String> keys = request.getParameterMap().keySet();
        for (String key : keys) {
            if (StringUtils.isEmpty(request.getParameter(key))) {
                continue;
            }
            parameterMap.put(key, request.getParameter(key));
        }
        return parameterMap;
    }

    /**
     * 从当前请求获取分页信息
     *
     * @return
     */
    public static PaginationInfo getPageInfoFromCurrentRequest() {
        Map<String, String> httpRequestParam = getParameterMap(getRequest());
        //如果current和per都没有，则认为是不分页的请求
        if (StringUtils.isBlank(httpRequestParam.get("current"))
                && StringUtils.isBlank(httpRequestParam.get("per"))) {
            return null;
        }

        PaginationInfo paginationInfo = new PaginationInfo();

        int pageNum = org.apache.commons.collections.MapUtils.getIntValue(httpRequestParam, "current", PaginationInfo.DEFAULT_PAGE_NUM);
        int pageSize = org.apache.commons.collections.MapUtils.getIntValue(httpRequestParam, "per", PaginationInfo.DEFAULT_PAGE_SIZE);
        if (pageSize == 0) {
            pageSize = PaginationInfo.DEFAULT_PAGE_SIZE;
        }
        paginationInfo.setPer(pageSize);
        paginationInfo.setCurrent(pageNum);

        return paginationInfo;
    }

    /**
     * 获取请求相关的log信息，用于记录到log
     * 目前包括的信息如下：
     * 1. 请求路径
     * 2. 请求参数
     *
     * @return
     */
    public static String getRequestLogString(HttpServletRequest request) {
        RequestLog requestLog = new RequestLog();
        requestLog.setUrl(request.getRequestURL().toString());
        requestLog.setParams(getCurrentParameterMap());
        return JSONObject.toJSONString(requestLog);
    }
}

/**
 * 这里写一个bean来标识log是因为我想把url字段输出在前面，用JSONObject.put方法输出的时候会排序
 */
class RequestLog {
    @JSONField(ordinal = 0)
    private String url;
    @JSONField(ordinal = 1)
    private Map<String, String> params;

    public RequestLog() {
    }

    public RequestLog(String url, Map<String, String> params) {
        this.url = url;
        this.params = params;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public Map<String, String> getParams() {
        return params;
    }

    public void setParams(Map<String, String> params) {
        this.params = params;
    }
}
    

